package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.MerchantPayQr;
import com.tiancheng.onecode.mapper.MerchantPayQrMapper;
import com.tiancheng.onecode.service.IMerchantPayQrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: kellen
 * @Description: 商户收款码管理
 * @Date: create in 2024/12/27 18:25
 */
@Slf4j
@Service
public class MerchantPayQrServiceImpl extends ServiceImpl<MerchantPayQrMapper, MerchantPayQr> implements IMerchantPayQrService {


    @Override
    public String genQr(MerchantPayQr merchantPayQr) {
        final List<MerchantPayQr> list = this.list(new LambdaQueryWrapper<MerchantPayQr>().eq(MerchantPayQr::getMerchantPayConfigId, merchantPayQr.getMerchantPayConfigId()).eq(MerchantPayQr::getQrStatus,0));
        if(CollectionUtils.isNotEmpty(list)){
            final List<MerchantPayQr> collect = list.stream().map(m -> {
                final MerchantPayQr update = new MerchantPayQr();
                update.setId(m.getId());
                update.setLastUpdDt(LocalDateTime.now());
                update.setQrStatus(1);
                return update;
            }).collect(Collectors.toList());
            this.updateBatchById(collect);
        }
        // 生成收款二维码
        this.save(merchantPayQr);
        return null;
    }

    @Override
    public void batchGenQr(List<MerchantPayQr> merchantPayQr) {
        final List<MerchantPayQr> list = this.list(new LambdaQueryWrapper<MerchantPayQr>().eq(MerchantPayQr::getMerchantPayConfigId, merchantPayQr.stream().map(MerchantPayQr::getMerchantPayConfigId).collect(Collectors.toList()))
                .eq(MerchantPayQr::getQrStatus,0));
        if(CollectionUtils.isNotEmpty(list)){
            final List<MerchantPayQr> collect = list.stream().map(m -> {
                final MerchantPayQr update = new MerchantPayQr();
                update.setId(m.getId());
                update.setLastUpdDt(LocalDateTime.now());
                update.setQrStatus(1);
                return update;
            }).collect(Collectors.toList());
            this.updateBatchById(collect);
        }
        // 保存收款二维码
        this.saveBatch(merchantPayQr);
    }
}
